<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="父节点" prop="parent_id">
        <el-select v-model="form.parent_id" placeholder="请选择父节点">
          <el-option
            :label="form.parent_name"
            style="height: auto; padding: 0"
            :value="form.parent_id"
          >
            <el-tree
              ref="treeRef"
              :data="treeData"
              default-expand-all
              :props="defaultProps"
              @node-click="handleNodeClick"
            />
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="副标题" prop="sub_title">
        <el-input v-model="form.sub_title" />
      </el-form-item>
      <el-form-item label="演讲嘉宾" prop="attend_guest_id">
        <el-select v-model="form.attend_guest_id" placeholder="请选择演讲嘉宾">
          <el-option
            v-for="item in guest"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="会场地点" prop="address">
        <el-input v-model="form.address" />
      </el-form-item>
      <!-- <el-form-item label="详情" prop="detail">
        <el-input v-model="form.detail" rows="5" type="textarea" />
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { defineComponent, getCurrentInstance, reactive, toRefs } from 'vue'
  import { doEdit, getList } from '@/api/schedule'

  export default defineComponent({
    name: 'RichengEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const { proxy } = getCurrentInstance()

      const state = reactive({
        formRef: null,
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'title',
          value: 'id',
        },
        form: {
          id: '',
          title: '',
          sub_title: '',
          parent_id: '',
          detail: '',
          parent_name: '',
          attend_guest_id: '',
        },
        rules: {
          parent_id: [
            { required: true, trigger: 'change', message: '请选择父节点' },
          ],
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        title: '',
        guest: [],
        dialogFormVisible: false,
      })

      const fetchData = async () => {
        console.log(state.form.metting_id)
        const {
          data: { list },
        } = await getList({ metting_id: state.form.metting_id })
        list.unshift({ title: '一级标题', id: 0 })
        state.treeData = list
      }
      const handleNodeClick = (node) => {
        state.form.parent_name = node.title
        state.form.parent_id = node.id
      }
      const showEdit = (row, guest) => {
        if (row.id) {
          state.title = '编辑'
          state.form = Object.assign({}, row)
        } else {
          state.title = '添加'
          state.form = Object.assign({}, row)
        }
        fetchData()
        state.guest = guest
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          parent_id: '',
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(state.form)
            fetchData()
            proxy.$baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      // fetchData()

      return {
        ...toRefs(state),
        handleNodeClick,
        showEdit,
        close,
        save,
      }
    },
  })
</script>
